using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CADTools
{
    public partial class ACIChooser : Form
    {
        #region " Declare! "

        private string[] KnownColor = new string[] { "Red", "Yellow", "Green", "Cyan", "Blue", "Magenta", "White" };
        private int[] XCornc = new int[] { 4, 17, 31, 45, 59, 73, 87, 100, 114, 128, 142, 156, 170, 183, 197, 211, 225, 239, 253, 266, 280, 294, 308, 322 };
        private int[] YCornc = new int[] { 52, 72, 40, 84, 28, 96, 16, 108, 4, 120 };
        private int[] XCornxEx = new int[] { 4, 21, 39, 57, 75, 93, 111, 129, 147 };
        static Bitmap Image19 = null;
        static Bitmap Image25 = null;
        static Bitmap ImagePlus = null;
        int ACIc = 0;
        int Aci = 0;  //Chu yeu la nhan mau tra ve khi select(khi Panel2.Clicked!) vi thang ACIc no xao xao, thay doi theo thang mouse move. Bo no biet!
        static Cursor Mcur;

        #endregion
        
        #region " Property "

        public int ColorIdex
        {
            get { return Aci; }
        }

        public Color RealColor
        {
            get { return this.PanCurCol.BackColor; }
        }

        public string ColorName
        {
            get { if (Aci <= 7)return KnownColor[Aci - 1]; else return Aci.ToString(); }
        }

        #endregion

        #region " Constructor! "

        public ACIChooser()
        {
            InitializeComponent();
        }

        public ACIChooser(int ColorIdx)
        {
            ACIc = ColorIdx;
            Aci = ColorIdx;
            InitializeComponent();
        }

        #endregion        

        #region " Loading! "

        private void ACIChooser_Load(object sender, EventArgs e)
        {
            ImagePlus = new Bitmap(this.pictureBox1.Image);
            Image19 = new Bitmap(this.pictureBox2.Image);
            Image25 = new Bitmap(this.pictureBox3.Image);
            Mcur = new Cursor(Application.StartupPath + "\\NoI.Ink");
            panel2.Cursor = Mcur;
            pictureBox1.Cursor = Mcur;
            pictureBox3.Cursor = Mcur;
            pictureBox3.Cursor = Mcur;
            this.PanSlcCor.Cursor = Mcur;
            this.PanSlcCorFull.Cursor = Mcur;

            //Set Status of Color Choosen!
            SetColorChoosed();
        }

        void SetColorChoosed()
        {
            if (ACIc == 0)
                return;

            Bitmap bm = null;
            int x = 4;
            int y = 4;

            if (ACIc >= 10 && ACIc <= 249)
            {
                bm = new Bitmap(this.pictureBox1.Image); //Acic = 23
                y = YCornc[ACIc % 10];
                x = XCornc[(ACIc - (ACIc % 10)) / 10 -1];

                this.PanSlcCorFull.Left = x - 4 + pictureBox1.Left;
                this.PanSlcCorFull.Top = y - 4 + pictureBox1.Top;
            }
            if (ACIc >= 1 && ACIc <= 9)
            {
                bm = new Bitmap(this.pictureBox2.Image); //Acic = 23
                x = XCornxEx[ACIc - 1];
                PanSlcCorFull.Width += 4;
                PanSlcCorFull.Height += 4;

                this.PanSlcCorFull.Left = x - 3 + pictureBox2.Left;
                this.PanSlcCorFull.Top = y - 3 + pictureBox2.Top;
            }
            if (ACIc >= 250 && ACIc <= 255)
            {
                bm = new Bitmap(this.pictureBox3.Image); //Acic = 23
                x = XCornxEx[ACIc - 250];
                PanSlcCorFull.Width += 4;
                PanSlcCorFull.Height += 4;
                this.PanSlcCorFull.Left = x - 3 + pictureBox3.Left;
                this.PanSlcCorFull.Top = y - 3 + pictureBox3.Top;
            }

            this.PanLastCol.BackColor = bm.GetPixel(x, y);
            this.PanSlcCor.BackColor = bm.GetPixel(x, y);
            this.PanCurCol.BackColor = bm.GetPixel(x, y);

            if (Aci <= 7)
                this.textBox1.Text = KnownColor[Aci - 1];           
            else
                this.textBox1.Text = Aci.ToString();
        }

        #endregion
        
        #region " Instect Pixel "

        int LeftOut(int pX, int pY, Color cur)
        {
            if (ImagePlus.GetPixel(pX, pY).Equals(cur))
                return LeftOut(pX - 1, pY, cur);
            return pX + 1;
        }

        int RightOut(int pX, int pY, Color cur)
        {
            if (ImagePlus.GetPixel(pX, pY).Equals(cur))
                return RightOut(pX + 1, pY, cur);
            return pX - 1;
        }

        int TopOut(int pX, int pY, Color cur)
        {
            if (ImagePlus.GetPixel(pX, pY).Equals(cur))
                return TopOut(pX, pY - 1, cur);
            return pY + 1;
        }

        int BottomOut(int pX, int pY, Color cur)
        {
            if (ImagePlus.GetPixel(pX, pY).Equals(cur))
                return BottomOut(pX, pY + 1, cur);
            return pY - 1;
        }

        //1-9
        int LeftOut19(int pX, int pY, Color cur)
        {
            if (Image19.GetPixel(pX, pY).Equals(cur))
                return LeftOut19(pX - 1, pY, cur);
            return pX + 1;
        }

        int RightOut19(int pX, int pY, Color cur)
        {
            if (Image19.GetPixel(pX, pY).Equals(cur))
                return RightOut19(pX + 1, pY, cur);
            return pX - 1;
        }

        int TopOut19(int pX, int pY, Color cur)
        {
            if (Image19.GetPixel(pX, pY).Equals(cur))
                return TopOut19(pX, pY - 1, cur);
            return pY + 1;
        }

        int BottomOut19(int pX, int pY, Color cur)
        {
            if (Image19.GetPixel(pX, pY).Equals(cur))
                return BottomOut19(pX, pY + 1, cur);
            return pY - 1;
        }

        //25
        int LeftOut25(int pX, int pY, Color cur)
        {
            if (Image25.GetPixel(pX, pY).Equals(cur))
                return LeftOut25(pX - 1, pY, cur);
            return pX + 1;
        }

        int RightOut25(int pX, int pY, Color cur)
        {
            if (Image25.GetPixel(pX, pY).Equals(cur))
                return RightOut25(pX + 1, pY, cur);
            return pX - 1;
        }

        int TopOut25(int pX, int pY, Color cur)
        {
            if (Image25.GetPixel(pX, pY).Equals(cur))
                return TopOut25(pX, pY - 1, cur);
            return pY + 1;
        }

        int BottomOut25(int pX, int pY, Color cur)
        {
            if (Image25.GetPixel(pX, pY).Equals(cur))
                return BottomOut25(pX, pY + 1, cur);
            return pY - 1;
        }

        #endregion

        #region " Mouse Move Drawing! "

        //ACI 10 - 249!!!
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Mcur;
            try //235,233,237!
            {
                if (pictureBox1.Image != null && ImagePlus.Width > e.X && ImagePlus.Height > e.Y)
                {
                    Color boja = ImagePlus.GetPixel(e.X, e.Y);
                    
                    label1.Text = "RED, GREEN, BLUE: " + boja.R.ToString() + "," + boja.G.ToString() + "," + boja.B.ToString();
                    
                    this.panel3.Visible = false;

                    if (boja != Color.FromArgb(235, 233, 237)) //Mau vien of PictureBox!
                    {
                        int tp = TopOut(e.X, e.Y - 1, boja);
                        int bt = BottomOut(e.X, e.Y + 1, boja);
                        int lf = LeftOut(e.X - 1, e.Y, boja);
                        int rg = RightOut(e.X + 1, e.Y, boja);
                        ACIc = 10;
                        //Lay Mau ACI!!!
                        for (int i = 0; i < XCornc.Length; i++)
                        {
                            if (lf == XCornc[i])
                            {
                                ACIc += i * 10;
                                break;
                            }
                        }
                        for (int i = 0; i < YCornc.Length; i++)
                        {
                            if (tp == YCornc[i])
                            {
                                ACIc += i;
                                break;
                            }
                        }

                        //LblTemp.Text = lf + ", " + tp + " -- " + (bt - tp).ToString() + " - " + (rg - lf).ToString();
                        label4.Text = "Index Color: " + ACIc;
                        panel2.BackColor = boja;
                        panel2.Visible = true;
                        this.panel2.SetBounds(lf + pictureBox1.Left - (18 - (rg - lf)) / 2, tp + pictureBox1.Top - (18 - (bt - tp)) / 2, 18, 18);
                        this.panel3.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //ACI 1 - 9!!!
        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Mcur;
            try //236,233,216!
            {
                if (pictureBox2.Image != null && Image19.Width > e.X && Image19.Height > e.Y)
                {
                    Color boja = Image19.GetPixel(e.X, e.Y);
                    
                    label1.Text = "RED, GREEN, BLUE: " + boja.R.ToString() + "," + boja.G.ToString() + "," + boja.B.ToString();
                    this.panel3.Visible = false;
                    if (boja != Color.FromArgb(236, 233, 216)) //Mau vien of PictureBox!
                    {
                        int tp = TopOut19(e.X, e.Y - 1, boja);
                        int bt = BottomOut19(e.X, e.Y + 1, boja);
                        int lf = LeftOut19(e.X - 1, e.Y, boja);
                        int rg = RightOut19(e.X + 1, e.Y, boja);
                        ACIc = 0;
                        //Lay Mau ACI!!!
                        for (int i = 0; i < XCornxEx.Length; i++)
                        {
                            if (lf == XCornxEx[i])
                            {
                                ACIc = i + 1;
                                break;
                            }
                        }

                        //LblTemp.Text = lf + ", " + tp + " -- " + (bt - tp).ToString() + " - " + (rg - lf).ToString();
                        label4.Text = "Index Color: " + ACIc;
                        panel2.BackColor = boja;
                        panel2.Visible = true;
                        this.panel2.SetBounds(lf + pictureBox2.Left - (22 - (rg - lf)) / 2, tp + pictureBox2.Top - (22 - (bt - tp)) / 2, 22, 22);
                        this.panel3.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //ACI 2 - 5!!!
        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Mcur;
            try //235,233,237!
            {
                if (pictureBox3.Image != null && Image25.Width > e.X && Image25.Height > e.Y)
                {
                    Color boja = Image25.GetPixel(e.X, e.Y);
                    
                    label1.Text = "RED, GREEN, BLUE: " + boja.R.ToString() + "," + boja.G.ToString() + "," + boja.B.ToString();
                    this.panel3.Visible = false;
                    if (boja != Color.FromArgb(236, 233, 216)) //Mau vien of PictureBox!
                    {
                        int tp = TopOut25(e.X, e.Y - 1, boja);
                        int bt = BottomOut25(e.X, e.Y + 1, boja);
                        int lf = LeftOut25(e.X - 1, e.Y, boja);
                        int rg = RightOut25(e.X + 1, e.Y, boja);
                        ACIc = 0;
                        //Lay Mau ACI!!!
                        for (int i = 0; i < XCornxEx.Length; i++)
                        {
                            if (lf == XCornxEx[i])
                            {
                                ACIc = i + 250;
                                break;
                            }
                        }

                        //LblTemp.Text = lf + ", " + tp + " -- " + (bt - tp).ToString() + " - " + (rg - lf).ToString();
                        label4.Text = "Index Color: " + ACIc;
                        panel2.BackColor = boja;
                        panel2.Visible = true;
                        this.panel2.SetBounds(lf + pictureBox3.Left - (22 - (rg - lf)) / 2, tp + pictureBox3.Top - (22 - (bt - tp)) / 2, 22, 22);
                        this.panel3.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        

        #endregion

        #region " Linh Tinh!!! "

        private void panel2_MouseLeave(object sender, EventArgs e)
        {
            panel2.Visible = false;
        }

        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            this.panel3.Visible = true;
        }

        private void PanSlcCor_MouseMove(object sender, MouseEventArgs e)
        {
            this.panel2.Visible = true;
            this.panel2.Left = PanSlcCorFull.Left + 1;
            this.panel2.Top = PanSlcCorFull.Top + 1;
            this.panel2.BackColor = PanSlcCor.BackColor;
            label1.Text = "RED, GREEN, BLUE: " + panel2.BackColor.R.ToString() + "," + panel2.BackColor.G.ToString() + "," + panel2.BackColor.B.ToString();
            label4.Text = "Index Color: " + Aci;
        }

        private void PanSlcCorFull_MouseMove(object sender, MouseEventArgs e)
        {
            this.panel2.Visible = true;
            this.panel2.Left = PanSlcCorFull.Left + 1;
            this.panel2.Top = PanSlcCorFull.Top + 1;
            this.panel2.BackColor = PanSlcCor.BackColor;
            label1.Text = "RED, GREEN, BLUE: " + panel2.BackColor.R.ToString() + "," + panel2.BackColor.G.ToString() + "," + panel2.BackColor.B.ToString();
            label4.Text = "Index Color: " + Aci;
        }        

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            //panel2.Visible = false;
            this.Cursor = Cursors.Default;
            this.panel3.Visible = false; //Hop thong so mau!
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != 13 && e.KeyChar != 8)
                e.Handled = true;
            if (e.KeyChar == 13)
            {
                try
                {
                    ACIc = int.Parse(this.textBox1.Text.Trim());
                    SetColorChoosed();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void panel2_Click(object sender, EventArgs e)
        {            
            if (ACIc != 0)
            {
                if (ACIc < 10 || ACIc > 249)
                {
                    PanSlcCorFull.Width = 24;
                    PanSlcCorFull.Height = 24;
                }
                else
                {
                    PanSlcCorFull.Width = 20;
                    PanSlcCorFull.Height = 20;
                }
                
                if (ACIc <= 7)
                    this.textBox1.Text = KnownColor[ACIc - 1];
                //else if (ACIc == 255)
                //    this.textBox1.Text = KnownColor[6];
                else
                    this.textBox1.Text = ACIc.ToString();

                this.PanCurCol.BackColor = panel2.BackColor;
                this.PanSlcCor.BackColor = panel2.BackColor;
                this.PanSlcCorFull.Left = panel2.Left - 1;
                this.PanSlcCorFull.Top = panel2.Top - 1;
                Aci = ACIc;
            }
        }

        #endregion                  
    }
}

    /*System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
      String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);*/